//
//  BannerCollectionViewCell.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

class BannerCollectionViewCell: UICollectionViewCell {
    
    private weak var _delegate: UICollectionViewCellDelegate?
    
    var cellModel: BannerAdCellModel? { didSet {
        
        guard let model = cellModel else { return }
        print("-->toj: func:\(#function), number:\(model.number)")
        numberLabel.text = model.number
        
        guard model.isRequest else {
            model.requestAdInfo { [weak self] cModel in
                self?.reloadCell(cModel)
            }
            return
        }
    } }
    
    func reloadCell(_ model: BannerAdCellModel) {
        guard model.isRequest, cellModel == model else { return }
        
        
        guard model.isSetNeedsLayout else { return }

        guard let adView = model.adView else { return }
        if _adView != adView {
            adView.removeFromSuperview()
            _adView = adView
        }
        adView.frame.origin = CGPoint.zero
        adContentView.addSubview(adView)
        
        
        //delegate?.collectionViewReloadCell(self)
        delegate?.collectionViewInvalidateLayout()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        print("-->toj: func: prepareForReuse BannerAdCell")
        cellModel?.resetCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /* numberLabel */
    private lazy var numberLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()

    /* adContentView */
    private lazy var adContentView: UIView = {
        let view = UIView()
        return view
    }()
    
    weak var _adView: UIView?
    
    deinit {
        print("-->toj: class: \(cm_objectName), func: \(#function)")
    }
}

extension BannerCollectionViewCell: UICollectionViewCellDataSource {
    
    var delegate: UICollectionViewCellDelegate? {
        get {
            return _delegate
        }
        set {
            _delegate = newValue
        }
    }
    
    var dataModel: UICollectionViewCellModelProtocol? {
        get {
            return cellModel
        }
        set {
            cellModel = newValue as? BannerAdCellModel
        }
    }
}

extension BannerCollectionViewCell {
    
    private func setupMakeAddSubviews() {
        contentView.addSubview(adContentView)
        contentView.addSubview(numberLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        numberLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        adContentView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        contentView.backgroundColor = .random
    }
}
